<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 用ES6中class的写法完成工厂类
        class User {
            constructor(role, pages) {
                this.role = role;
                this.pages = pages;
            }

            static UserFactory(role) {
                switch (role) {
                    case "user": {
                        return new User('user', ['home']);
                        break;
                    }
                    case "super-admin": {
                        return new User('super-admin', ['home', 'user-manage']);
                        break;
                    }
                    default: {
                        throw new Error('no such role as' + role);
                    }
                }
            }
        }

        let user = User.UserFactory('user');
        console.log(user);
    </script>
</body>

</html>